package com.dev.security.manager.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.dev.security.config.dao.BaseRepository;
import com.dev.security.manager.beans.Permission;
import com.dev.security.manager.beans.RolePermUnit;
import com.dev.security.manager.beans.RolePermUnitId;

/**
 * @author 潜行的青衣
 * @see
 */
public interface RolePermUnitRepository extends BaseRepository<RolePermUnit, RolePermUnitId> {

	@Query("select rpu from RolePermUnit rpu where role_id=?1 order by rpu.permission.index")
	public List<RolePermUnit> findAll(String role_id);
	
	@Query("select rpu.permission.id from RolePermUnit rpu where role_id=?1 order by rpu.permission.index")
	public List<String> findAllPerId(String role_id);
	
	@Query("select rpu.permission from RolePermUnit rpu where role_id in ?1 and rpu.permission.app_id=?2 order by rpu.permission.index")
	public List<Permission> findAllPermissions(List<String> role_ids, String app_id);
	
	@Query("select count(rpu) from RolePermUnit rpu where role_id in ?1 and rpu.permission.id=?2")
	public long countPermissions(List<String> role_ids, String per_id);
	
	@Modifying
	@Query("delete from RolePermUnit where role_id=?1")
	public void delete(String role_id);
	
	@Modifying
	@Query("delete from RolePermUnit where per_id=?1")
	public void deleteByPermission(String per_id);
}
